var app = getApp();
var util = require('../../utils/util.js');
Page({
  data: {
    //搜索删除标记
    deleteFlag: true,
    //喵文列表
    talkList: [],
    talkList1: [],
    talkList2: [],
    //喵文隐藏
    talkListHidden: true,
    //搜索记录
    searchRecord: [],
    //热门搜索
    hotSearch: [],
    //输入搜索内容
    keyword: '',
    //当前页
    pageNumber: 0,
    //最后时间
    time: '',
    //没有数据提示
    noTalkShow: true
  },
  onLoad: function(options) {
    //设置顶部返回按钮
    this.setData({
      statusBarHeight: app.globalData.systemInfo.statusBarHeight + 15
    })
    //搜索历史
    this.searchRecord();
    //热门搜索
    this.getSearchHotList();
  },
  //上拉加载
  onReachBottom: function() {
    var talkList = this.data.talkList;
    //最后一个喵文
    var talk = talkList[talkList.length - 1];
    this.data.time = talk.talkDate;
    //搜索
    this.getTalkSearch();
  },

  //删除搜索记录
  clearRecord: function() {
    this.setData({
      searchRecord: []
    })
    wx.removeStorage({
      key: 'searchRecord'
    })
  },

  //跳转详情
  showTalk: function(e) {
    var id = e.detail.currentTarget.dataset.id;
    var label = e.detail.currentTarget.dataset.label;
    var userId = app.globalData.user.id;
    var type = e.detail.currentTarget.dataset.type;
    if (type == 1) {
      wx.navigateTo({
        url: '/pages/imageShow/imageShow?id=' + id + '&type=' + type + '&label=' + label,
      })
    } else {
      wx.navigateTo({
        url: '/pages/videoShow/videoShow?id=' + id + '&type=' + type + '&label=' + label,
      })
    }

  },
  //获取搜索记录
  searchRecord: function() {
    var searchRecord = wx.getStorageSync('searchRecord');
    if (searchRecord != '') {
      this.setData({
        searchRecord: JSON.parse(searchRecord)
      })
    }
  },
  //点击记录搜索
  clickSearch: function(e) {
    var keyword = '';
    var type = e.currentTarget.dataset.type;
    var index = e.currentTarget.dataset.index;
    if (type == 1) {
      var searchRecord = this.data.searchRecord;
      keyword = searchRecord[index];
    } else {
      var hotSearch = this.data.hotSearch;
      keyword = hotSearch[index].keyword;
    }
    this.setData({
      talkList: [],
      keyword: keyword,
      talkListHidden: false,
      deleteFlag: false
    })
    //搜索
    this.getTalkSearch();
  },
  //键入输入内容
  searchInput: function(e) {
    var cursor = e.detail.detail.cursor;
    var value = e.detail.detail.value;
    if (cursor > 10) {
      value = value.substr(0, 10);
    }
    this.setData({
      keyword: value
    })
  },
  //确定输入内容
  searchSubmit: function() {
    //键入关键字
    var keyword = this.data.keyword;
    if (keyword == '') {
      return;
    }
    //搜索记录
    var searchRecord = this.data.searchRecord;
    this.setData({
      deleteFlag: false
    })
    //如果记录超过10个则删除最后一个
    if (searchRecord.length > 9) {
      searchRecord.splice(9, 1);
    }
    //添加记录
    searchRecord.unshift(keyword);
    wx.setStorage({
      key: 'searchRecord',
      data: JSON.stringify(searchRecord),
    })
    //重新刷新搜索记录
    this.searchRecord();
    this.setData({
      talkListHidden: false,
      talkList: [],
      talkList1: [],
      talkList2: [],
      noTalkShow: true
    })

    //搜索
    this.getTalkSearch();
  },
  //清除输入内容
  clearInput: function() {
    this.setData({
      deleteFlag: true,
      talkListHidden: true,
      talkList: [],
      keyword: '',
      noTalkShow: true
    })
  },
  //搜索数据
  getTalkSearch: function() {
    var that = this;
    wx.request({
      url: app.globalData.requestUrl + 'getTalkListByKeyword',
      data: {
        userId: app.globalData.user.id,
        keyword: that.data.keyword,
        time: that.data.time
      },
      method: 'GET',
      success: function(r) {
        var data = r.data.data;
        if (data.length == 0) {
          if (that.data.talkList.length == 0) {
            that.setData({
              noTalkShow:false
            })
          } else {
            wx.showToast({
              title: '只帮您搜索到这么多日志Q-Q',
              icon: 'none',
              duration: 3000
            })
          }
        } else {
          //左边数组
          var arr1 = [];
          //右边数组
          var arr2 = [];
          for (var i = 0; i < data.length; i++) {
            //转换数字
            data[i].likeQuantity = util.formatQuantity(data[i].likeQuantity);
            //找到图片高度
            data[i].imageHeight = util.getImageHeight(data[i].height, data[i].width, 0.48);
            //如果是视频,则取视频封面
            if (data[i].talkType != 0) {
              data[i].src = data[i].src + "?x-oss-process=video/snapshot,t_1000,f_jpg,w_" + data[i].width + ",h_" + data[i].height + ",m_fast";
            }
            //如果是双数则放到第一个盒子
            if (i % 2 == 0) {
              arr1.push(data[i]);
            } else {
              arr2.push(data[i]);
            }
          }
          //如果是下拉则清空
          if (that.data.refresh) {
            that.setData({
              talkList: data,
              talkList1: arr1,
              talkList2: arr2,
              refresh: false
            })
            wx.stopPullDownRefresh();
          } else {
            that.setData({
              talkList: that.data.talkList.concat(data),
              talkList1: that.data.talkList1.concat(arr1),
              talkList2: that.data.talkList2.concat(arr2)
            })
          }
        }
      },
      fail: function() {
        wx.showToast({
          title: '获取日志失败,请下拉重试',
          icon: 'none',
          duration: 30000
        })
      }
    })


  },
  //获取热门搜索
  getSearchHotList: function() {
    var that = this;
    wx.request({
      url: app.globalData.requestUrl + 'getSearchRecordList',
      method: 'GET',
      success: function(r) {
        if (r.data.code == 200) {
          that.setData({
            hotSearch: r.data.data
          })
        }
      }
    })
  },
  //返回下一个页面
  rollback: function() {
    //获取页面值栈
    var pages = getCurrentPages();
    wx.navigateBack({
      delta: 1
    })
  }
})